package com.homework.im.config;

import com.homework.im.handler.MsgHandlerFactory;
import com.homework.im.server.ImServerStarter;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

@Slf4j
@Data
@Configuration
public class ImServerAutoConfig {

    @Value("${im.server.port}")
    private Integer imPort;

    @Bean
    public ImServerStarter imServerStarter() {

        try {
            ImServerStarter imServerStarter = new ImServerStarter(imPort);
            imServerStarter.start();

            //初始化消息处理器工程
            MsgHandlerFactory.init();

            log.info("---------> im server started !");
            return imServerStarter;

        } catch (IOException e) {
            log.error("im server 启动失败~~", e);
        }
        return null;
    }

}
